<?php

namespace App\Http\Controllers\Admin;

use App\Model\Category;
use App\Model\Meta;
use App\Model\Page;
use App\Services\CategoryService;
use App\Services\Util;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class PageController extends AdminController
{
    public function index()
    {
        return view('admin.page.index');
    }

    public function add($type = null)
    {
        if($this->request->isMethod('get')){
            $orm = new Page();
            $orm = $orm->where('state','!=',Page::STATE_DRAFT);
            switch ($type){
                case Page::TYPE_PAGE:
                    $pages = $orm->where('level',1)->get(['id','title']);
                    return view('admin.page.add',['pages'=>$pages,'type'=>$type]);
                case Page::TYPE_POST:
                    $cateModel = new Category();
                    $categorys = $cateModel->where('type',Category::TYPE_CATE)->get();
                    $tags      = $cateModel->where('type',Category::TYPE_TAG)->get();
                    return view('admin.page.add',['categorys'=>$categorys,'tags'=>$tags,'type'=>$type]);
                default:
                    return self::error('系统错误');
            }
        }

        if($this->request->isMethod('post')){
//            $rules = [
//                'title'      => 'required',
//                'url'        => 'required',
//                'author'     => 'required',
//                'state'      => 'required',
//            ];
//            $validator = Validator::make(Input::all(), $rules);
//            if ($validator->fails()) {
//                return self::apiFail("请填写参数");
//            }
            DB::beginTransaction();
            $page = new Page();
            $meta = new Meta();
            $page->type  = Input::get('data')['type'];
            $page->title = Input::get('data')['title'];
            $orm = new Page();
            switch ($page->type){
                case Page::TYPE_PAGE:
                    $page->parent  = Input::get('data')['parent'];
                    $page->right   = Input::get('data')['right'];
                    $page->url     = Input::get('data')['url'];
                    $orm   = $orm->where('type',Page::TYPE_PAGE);
                    $exits = $orm->where('title',$page->title)->first();
                    if(empty($exits)){
                        $page->url = Util::domain($page->url);
                        $exits = $orm->where('url',$page->url)->first();
                        if(!empty($exits))
                        {
                            return self::apiFail('页面链接重复');
                        }
                    }else{
                        return self::apiFail('存在同名页面');
                    }
                    if(empty($page->parent)||$page->parent == 1) {
                        $page->level = 1;
                    }else{
                        $level = Page::where('id',$page->parent)->first();
                        if(empty($level))
                        {
                            return self::apiFail('参数错误');
                        }
                        $page->level = (int)$level->level + 1;
                    }
                    $meta->icon = Input::get('data')['icon'];
                    break;
                case Page::TYPE_POST:
                    $meta->categorys = Input::get('data')['categorys'];
                    $meta->categorys = json_encode($meta->categorys);
                    $meta->tags = Input::get('data')['tags'];
                    $meta->tags = json_encode($meta->tags);
                    $orm   = $orm->where('type',Page::TYPE_PAGE);
                    $exits = $orm->where('title',$page->title)->first();
                    if(!empty($exits)){
                        return self::apiFail('存在同名文章');
                    }
            }
            $meta->img     = Input::get('data')['img'];
            $page->secret  = Input::get('data')['secret'];
            $page->content = Input::get('data')['content'];
            $page->state   = Input::get('data')['state'];
            $page->author  = $this->getEmployeeId();

            if($page->getStateName() == '未知'){
                $page->state = Page::STATE_CHECK;
            }

            $page->save();
            $meta->page_id = $page->id;
            $meta->save();
            if($page->type == Page::TYPE_POST){
                $cateSrv = new CategoryService();
                $cateSrv->countUsed($page,Category::TYPE_CATE);
                $cateSrv->countUsed($page,Category::TYPE_TAG);
            }
            DB::commit();
            return self::apiSucc('添加成功');
        }
    }
}
